﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TravelLink.WebModel;

namespace TravelLink.WebApp.Ajax {
    public partial class User_AjaxHandler : AjaxPage {
        const int Valid_ValidatorCode_Command = 1000;
        const int Valid_UserExists_Command = 1001;
        const int Valid_UserPassword_Command = 1002;

        protected void Page_Load(object sender, EventArgs e) {
            int command = WebHelper.GetQueryStringInt("cmd");
            switch (command) {
                case Valid_ValidatorCode_Command:
                    ValidCode();
                    break;
                case Valid_UserExists_Command:
                    ValidUserExists();
                    break;
                case Valid_UserPassword_Command:
                    ValidUserPassword();
                    break;
                default:
                    Error();
                    break;
            }
            Response.End();

        }


        private void ValidCode() {
            string code = WebHelper.GetQueryStringStr(WebHelper.GetQueryStringStr("rn"));
            if (this.GetValidatorCode(WebHelper.GetQueryStringStr("t")).Equals(code, StringComparison.CurrentCultureIgnoreCase)) {
                Response.Write("200");
            }
            else {
                Response.Write("500");
            }
        }

        private void ValidUserExists() {
            string email = WebHelper.GetQueryStringStr(WebHelper.GetQueryStringStr("rn"));
            var userEntity = DB.TL_User.Where(u => u.Email.ToUpper().Trim() == email.ToUpper().Trim()).FirstOrDefault();
            if (userEntity == null) {
                Response.Write("200");
            }
            else {
                Response.Write("500");
            }
        }

        private void ValidUserPassword() {
            string pwd = WebHelper.GetQueryStringStr(WebHelper.GetQueryStringStr("rn"));
            var isEqual = this.CurrentUser.DBUser.Password.Equals(pwd, StringComparison.CurrentCultureIgnoreCase);
            if (isEqual) {
                Response.Write("200");
            }
            else {
                Response.Write("500");
            }
        }
    }
}